﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyDonVi : BasePage
    {
        DonViBLL dvBLL = new DonViBLL();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnThemCV_Click(object sender, EventArgs e)
        {
            try
            {
                DONVI dv = new DONVI();
                int _madonvi = int.Parse(txtMaDonVi.Text);
                string _tendonvi = txtTenDonVi.Text.Trim();

                if (dvBLL.KiemTraTonTai(_madonvi, _tendonvi) == null)
                {
                    dv.MaDonVi = _madonvi;
                    dv.TenDonVi = _tendonvi;
                    dv.MoTa = txtMoTa.Text.Trim();
                    dvBLL.Them(dv);
                    lbthongbao.Text = "Thêm thành công!";
                    dgvDSDonVi.DataBind();

                    Reset();
                }
                else
                {
                    lbthongbao.Text = dvBLL.KiemTraTonTai(_madonvi, _tendonvi) + " đã tồn tại";
                }
            }
            catch { }
        }

        protected void Reset()
        {
            txtMaDonVi.Text = "";
            txtTenDonVi.Text = "";
            txtMoTa.Text = "";
        }

        protected void dgvDSDonVi_rowdatabound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["MaDonVi"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["MaDonVi"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int ma = int.Parse(btn.Attributes["MaDonVi"]);

            DONVI a = dvBLL.LayDonVi(ma);

            txtMaDonVi.Text = a.MaDonVi.ToString();
            txtTenDonVi.Text = a.TenDonVi.ToString();
            txtMoTa.Text = a.MoTa.ToString();

            txtMaDonVi.Enabled = false;
            txtTenDonVi.Enabled = false;
            txtMoTa.Enabled = false;
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int ma = int.Parse(btn.Attributes["MaDonVi"]);
            try
            {
                dvBLL.Xoa(ma);
                dgvDSDonVi.DataBind();
                lbThongbao2.Text = "Xóa thành công!";
            }
            catch { }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Reset();

            txtMaDonVi.Enabled = true;
            txtTenDonVi.Enabled = true;
            txtMoTa.Enabled = true;
        }
    }
}